package jp.co.systemi.asp.workflow.peer;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import jp.co.systemi.asp.workflow.data.Group;
import jp.co.systemi.asp.workflow.data.GroupMeta;

import org.apache.commons.lang.StringUtils;
import org.slim3.datastore.Datastore;
import org.slim3.datastore.FilterCriterion;
import org.slim3.datastore.ModelQuery;

public class GroupPeer{
    @SuppressWarnings("unused")
    private static final Logger log = Logger.getLogger(GroupPeer.class
            .getName());

    public static List<Group> doSearch(Group model){
        GroupMeta gm = GroupMeta.get();
        List<FilterCriterion> fcList = new ArrayList<FilterCriterion>();
        if(model != null && !StringUtils.isEmpty(model.getCode())){
            fcList.add(gm.code.equal(model.getCode()));
        }
        if(model != null && !StringUtils.isEmpty(model.getName())){
            fcList.add(gm.name.equal(model.getName()));
        }
        if(model != null && !StringUtils.isEmpty(model.getParentGroup())){
            fcList.add(gm.parentGroup.equal(model.getParentGroup()));
        }
        ModelQuery<Group> query = Datastore.query(gm);
        if(fcList.size() > 0){
            FilterCriterion[] fcArray = new FilterCriterion[fcList.size()];
            fcArray = fcList.toArray(fcArray);
            query = query.filter(fcArray);
        }
        return query.sort(gm.parentGroup.asc, gm.seq.asc).asList();
    }

}
